import { Component, OnInit, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
import { NgForm, FormBuilder, Validators, AbstractControl, FormGroupDirective, FormArray } from '@angular/forms';
import _ from 'lodash';
import { ZoneService, ZonePersonnel, Zone } from 'app/modules/zone/services/zone.service';
import { TaskPeriod, TaskTarget } from 'app/modules/task/services/task.service';
import { ClientMessageService, ClientMessageTask } from '../../services/client-message.service';
import { ToasterService } from 'app/modules/share/services/toaster.service';
import { markAllAsTouched, firstInvalid } from 'app/modules/base/helper';
import { DatePipe } from '@angular/common';
import * as dayjs from 'dayjs';
@Component({
	selector: 'm-client-message-send',
	templateUrl: './client-message-send.component.html',
	styleUrls: ['./client-message-send.component.scss'],
	providers: [DatePipe],
})
export class ClientMessageSendComponent implements OnInit {
	@ViewChild(NgForm) ngForm: NgForm;
	readonly TaskPeriod = TaskPeriod;
	constructor(private clientMessageService: ClientMessageService, private toaster: ToasterService, private datePipe: DatePipe,) { }

	formGroup = this.clientMessageService.sendFormGroup();

	ngOnInit() {
		if (window['HACKER'].BX_MODE) {
			window['test'] = this
		}
	}

	async submit(submitBtn: HTMLButtonElement) {
		markAllAsTouched(this.formGroup);
		if (this.formGroup.invalid) {
			return;
		}
		submitBtn.disabled = true;
		const value: FormValue = this.formGroup.value;

		console.log('value---:', value)

		let title = value.title.trim();
		let content = value.content.trim();

		this.formGroup.patchValue({
			title,
			content,
		})
		const task: any = {
			// const task: ClientMessageTask = {
			period_type: value.period_type,
			target: TaskTarget.ZoneAndUser,
			// content: { title: value.title, message: value.content },
			content: { title: value.title, message: new Function('return this.trimStart()').call(value.content) },
			users: _.map(value.receiver.users, v => ({ id: v.id })),
			areas: _.map(value.receiver.zones, v => ({ id: v.id })),
		};
		if (!task.users.length && !task.areas.length) {
			return;
		}

		if (value.period_type === TaskPeriod.Timing) {
			task.planned_at = value.date_time;
		}
		if (value.period_type === TaskPeriod.Schedule) {
			// task.planned_at = value.week_time.time;

			const day = dayjs(value.week_time.time);
			const time = day.format('HH:mm');
			task.planned_at = new Date(dayjs(new Date()).format('YYYY-MM-DD') + ' ' + time)
			task.weekdays = value.week_time.weekdays;
		}




		// // const date = day.format('YYYY-MM-DD');
		// const time = day.format('YYYY-MM-DD HH:mm:ss');

		// console.log('value.week_time.time---:', value.week_time.time, time)


		// // new Function('')

		// task['planned_at'] = time;
		// task['planned_at'] = this.datePipe.transform(new Date(task.planned_at), 'yyyy-MM-dd hh:ii').toString();

		await this.clientMessageService.create(task);
		if (value.period_type === TaskPeriod.Now) {
			this.toaster.success('发送成功');
		} else {
			this.toaster.success('创建成功');
		}
		history.back();
	}
}

interface FormValue {
	period_type: TaskPeriod;
	title: string;
	content: string;
	date_time?: Date;
	receiver: {
		users: ZonePersonnel[];
		zones: Zone[];
	};
	week_time?: {
		weekdays: boolean[];
		time: Date;
	};
}
